package com.bjtu.douyin.controller;

import com.bjtu.common.auth.AuthRedisKey;
import com.bjtu.common.pojo.Comment;
import com.bjtu.common.pojo.Page;
import com.bjtu.common.util.Result;
import com.bjtu.common.util.ResultCode;
import com.bjtu.douyin.service.CommentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Api(tags = "评论增删改查")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 通过token获取用户id
     *
     * @param token 用户token
     * @return 用户id
     */
    public Integer getUserIdByToken(String token) {
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        return (Integer) ops.get(AuthRedisKey.TOKEN_TO_UID + token);
    }

    /**
     * 分页查询评论
     *
     * @param page 页码
     * @param pageSize 每页大小
     * @param videoId 视频id
     * @return 返回分页的评论数据
     */
    @GetMapping("/list")
    public Result<?> listComments(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam("videoId") Integer videoId,
                                  @RequestHeader(value = "token", required = false) String token) {
        Integer userId = null;
        if (token != null && !token.isEmpty()) {
            userId = getUserIdByToken(token);
            if (userId == null) {
                return Result.error(ResultCode.FAILED, "用户未登录");
            }
        }
        Page commentPage = commentService.page(page, pageSize, videoId, userId);
        return Result.success(commentPage);
    }

    /**
     * 添加评论
     *
     * @param content 评论内容
     * @param videoId 视频id
     * @param rootCommentId 顶级评论id
     * @param toCommentId 父评论id
     * @param token 请求头中的用户token
     * @return 返回处理结果信息
     */
    @PostMapping("")
    public Result<?> addComment(@RequestParam("content") String content,
                              @RequestParam("videoId") Integer videoId,
                              @RequestParam(value = "rootCommentId",defaultValue = "") Integer rootCommentId,
                              @RequestParam(value = "toCommentId",defaultValue = "") Integer toCommentId,
                              @RequestHeader("token") String token) {
        // 通过token获取用户id
        Integer userId = getUserIdByToken(token);

        // 构建video对象
        Comment comment = new Comment();
        comment.setStatus(1);
        comment.setLikeNumber(0);
        comment.setContent(content);
        comment.setVideoId(videoId);
        comment.setUserId(userId);
        comment.setCreateTime(LocalDateTime.now());
        comment.setRootCommentId(rootCommentId);
        comment.setToCommentId(toCommentId);
        return commentService.addComment(comment);
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return 返回处理结果信息
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteComment(@PathVariable Integer id,
                                   @RequestHeader("token") String token) {
        Integer userId = getUserIdByToken(token);
        if (userId == null) {
            return Result.error(ResultCode.FAILED, "用户未登录");
        }
        return commentService.deleteComment(id);
    }

    /**
     * 通过id获取单个评论
     */
    @GetMapping("/{id}")
    public Result<?> getComment(@PathVariable Integer id, @RequestHeader(value = "token", required = false) String token) {
        Integer userId = null;
        if (token != null && !token.isEmpty()) {
            userId = getUserIdByToken(token);
            if (userId == null) {
                return Result.error(ResultCode.FAILED, "用户未登录");
            }
        }
        return commentService.getCommentById(id,userId);
    }

    /**
     *  点赞评论
     * @param id 评论id
     * @return 返回处理结果信息
     */
    @PutMapping("/like")
    public Result<?> likeComment(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        Integer userId = getUserIdByToken(token);
        if (userId == null) {
            return Result.error(ResultCode.FAILED, "用户未登录");
        }
        return commentService.likeComment(id, userId);
    }

    /**
     *  取消点赞评论
     * @param id 评论id
     * @return 返回处理结果信息
     */
    @PutMapping("/dislike")
    public Result<?> dislikeComment(@RequestParam("id") Integer id, @RequestHeader("token") String token) {
        Integer userId = getUserIdByToken(token);
        if (userId == null) {
            return Result.error(ResultCode.FAILED, "用户未登录");
        }
        return commentService.dislikeComment(id, userId);
    }
}
